import PureMachinesForm from '../../components/unit-config-forms/machines';
import { getFetchStatus } from 'actions/util';
import * as RunnableGroupAction from 'actions/runnable-group';
import * as RunnableUnitAction from 'actions/runnable-unit';
import * as React from 'react';
import * as ReactRedux from 'react-redux';

const mapStateToProps = (state) => {
  return {
    fetching: getFetchStatus(state, RunnableGroupAction.listGroupMachines),
    updateMachinesFetching: getFetchStatus(state, RunnableUnitAction.updateUnitMachines),
    groupMachines: state.groupMachines
  };
};
const mapDispatchToProps = (dispatch, ownProps) => {
  const { groupId } = ownProps.unit;

  return {
    initFetch: () => {
      dispatch(RunnableGroupAction.listGroupMachines(groupId));
    },
    onMachinesUpdate: (unitId, runtimeId, machineIds) => {
      dispatch(RunnableUnitAction.updateUnitMachines(unitId, runtimeId, machineIds));
    }
  };
};

class MachinesForm extends React.Component {
  componentDidMount() {
    this.props.initFetch();
  }

  render() {
    return <PureMachinesForm {...this.props}/>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(MachinesForm);
